package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.MaterialsBI;
import com.cleaningcloud.cld.domain.SaleBI;
import com.cleaningcloud.cld.domain.vo.CldBiDataVo;
import com.cleaningcloud.cld.mapper.SaleBIMapper;
import com.cleaningcloud.cld.service.ISaleBIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @className 销售利润BI业务实现类
 * @description //TODO
 * @author Shaw
 * @date: 2025-03-06 16:33
 * @param: //TODO
 */
@Service
public class SaleBIServiceImpl implements ISaleBIService {

    //系统报表映射
    @Autowired
    private SaleBIMapper saleBIMapper;

    /**
     * @Author Shaw
     * @Description 库存统计表格
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getCommodityStockSaleByCommodityTypeTable() {
        return saleBIMapper.getCommodityStockSaleByCommodityTypeTable();
    }

    /**
     * @Author Shaw
     * @Description 商品公司库存占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getCompanyCommodityTypeSaleRate() {
        return saleBIMapper.getCompanyCommodityTypeSaleRate();
    }

    /**
     * @Author Shaw
     * @Description 按当月查询物资资产成本(日走势图)
     * @date: 2025-09-09 9:45
     * @param: //TODO
     */
    @Override
    public CldBiDataVo getMaterialsInfoSaleDayImg(){
        CldBiDataVo cbv = new CldBiDataVo();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        BigDecimal[] qjgj = new BigDecimal[days];
        BigDecimal[] qjyj = new BigDecimal[days];
        BigDecimal[] ljcz = new BigDecimal[days];
        BigDecimal[] czsn = new BigDecimal[days];
        BigDecimal[] jxpj = new BigDecimal[days];
        BigDecimal[] keyp = new BigDecimal[days];
        BigDecimal[] lbyp = new BigDecimal[days];
        BigDecimal[] qt = new BigDecimal[days];
        BigDecimal[] sum = new BigDecimal[days];
        SaleBI bi = new SaleBI();
        bi.setDateYear(year);
        bi.setDateMonth(month);
        for (int i=1 ; i<=days ; i++) {
            String dateVal = "";
            if(i < 10){
                if(month < 10){
                    dateVal = year + "0" + month + "0" + i;
                }else{
                    dateVal = year + "" + month + "0" + i;
                }
            }else{
                if(month < 10){
                    dateVal = year + "0" + month + "" + i;
                }else{
                    dateVal = year + "" + month + "" + i;
                }
            }
            bi.setDateVal(dateVal);
            SaleBI obj = saleBIMapper.getMaterialsInfoSaleDayImg(bi);

            qjgj[i-1] = obj.getQjgjSaleProfit();
            qjyj[i-1] = obj.getQjyjSaleProfit();
            ljcz[i-1] = obj.getLjczSaleProfit();
            czsn[i-1] = obj.getCzsnSaleProfit();
            jxpj[i-1] = obj.getJxpjSaleProfit();
            keyp[i-1] = obj.getKeypSaleProfit();
            lbyp[i-1] = obj.getLbypSaleProfit();
            qt[i-1] = obj.getQtSaleProfit();
            sum[i-1] = obj.getSumSaleProfit();

        }
        Object[] dataVal = new Object[9];
        dataVal[0] =  qjgj;
        dataVal[1] =  qjyj;
        dataVal[2] =  ljcz;
        dataVal[3] =  czsn;
        dataVal[4] =  jxpj;
        dataVal[5] =  keyp;
        dataVal[6] =  lbyp;
        dataVal[7] =  qt;
        dataVal[8] =  sum;
        cbv.setDataVal(dataVal);
        return cbv;
    }

    /**
     * @Author Shaw
     * @Description 按当月查询物资销售额/利润(月累计表格)
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getMaterialsInfoSaleTable() {
        return saleBIMapper.getMaterialsInfoSaleTable();
    }

    /**
     * @Author Shaw
     * @Description 商品类型销售数量占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getSaleCommodityTypeRate() {
        return saleBIMapper.getSaleCommodityTypeRate();
    }

    /**
     * @Author Shaw
     * @Description 商品类型销售数量/金额表格
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getSaleCommodityTypeTable() {
        return saleBIMapper.getSaleCommodityTypeTable();
    }

    /**
     * @Author Shaw
     * @Description 获取销售客户销售订单占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getSaleCustomerOrderRate() {
        List<SaleBI> list = saleBIMapper.getSaleCustomerOrderRate();
        BigDecimal saleMoney = new BigDecimal(0L);
        if(!list.isEmpty()){
            for (SaleBI saleBI : list) {
                saleMoney = saleMoney.add(saleBI.getSaleMoney());
            }
        }
        List<SaleBI> newList = saleBIMapper.getSaleCustomerOrderRate();
        if(!newList.isEmpty()){
            for (SaleBI saleBI : newList) {
                saleBI.setSaleTypeRate(saleBI.getSaleMoney().divide(saleMoney,2,BigDecimal.ROUND_HALF_UP));
            }
        }
        return newList;
    }

    /**
     * @Author Shaw
     * @Description 销售订单不同状态订单数表格
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String,Object> getSaleOrderInfoTable() {
        return saleBIMapper.getSaleOrderInfoTable();
    }

    /**
     * @Author Shaw
     * @Description 根据类型ID获取公司库存数量/金额占比
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String,Object> getCompanyStockRateByCommodityTypeId(SaleBI saleBI) {
        return saleBIMapper.getCompanyStockRateByCommodityTypeId(saleBI);
    }

    /**
     * @Author Shaw
     * @Description 根据类型ID获取公司库存数量/金额TOP20
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getCompanyStockTableByCommodityTypeId(SaleBI saleBI) {
        return saleBIMapper.getCompanyStockTableByCommodityTypeId(saleBI);
    }

    /**
     * @Author Shaw
     * @Description 根据类型ID获取销售成本/利润
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public Map<String,Object> getSaleMoneyByCommodityTypeId(SaleBI saleBI) {
        return saleBIMapper.getSaleMoneyByCommodityTypeId(saleBI);
    }

    /**
     * @Author Shaw
     * @Description 根据类型ID获取销售数量/金额TOP20
     * @date: 2025-03-06 16:37
     * @param: //TODO
     */
    @Override
    public List<SaleBI> getLevelSaleTableByCommodityTypeId(SaleBI saleBI) {
        return saleBIMapper.getLevelSaleTableByCommodityTypeId(saleBI);
    }
}